var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods:[],
    collect:'active',
    sort:'desc',
    request_num: 0,
    types:"",
    is_show_time: 1,
    currentNav: 'collect' // 添加当前导航状态，默认选中综合
  },
  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&typ=json&pagesize=10',
      method: 'GET',
      data: {
        shop_id: options.shop_id,
        id: options.shop_id,
        type_wxapp: 'wxapp'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
       
        var good_info = res.data.data.items
        var page = Number(res.data.data.page + 1)
      
        that.setData({
          goods: good_info,
          shop_id: options.shop_id,
          page:page,
          types: "",
          is_show_time: 0,
          currentNav: 'collect' // 默认选中综合
        })
      }
    })
  },

  //综合数据
  collect: function () {
    var that = this
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&typ=json&pagesize=10',
      method: 'GET',
      data: {
        shop_id: that.data.shop_id,
        id: that.data.shop_id,
        type_wxapp: 'wxapp'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items
        that.setData({
          goods: good_info,
          collect: "active",
          salenum: "",
          price: "",
          types:"",
          request_num: 0,
          currentNav: 'collect' // 更新导航状态
        })
      }
    })
  },

  //获取销量数据
  salenum: function () {
    var that = this
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&sort=desc&typ=json&pagesize=1000',
      method: 'GET',
      data: {
        id: that.data.shop_id,
        order: 'common_salenum',
        type_wxapp: 'wxapp'
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items
        that.setData({
          goods: good_info,
          collect: "",
          salenum: "active",
          price:"",
          types:"common_salenum",
          request_num: 0,
          currentNav: 'salenum' // 更新导航状态
        })
      }
    })
  },

  //根据价格排序
  price:function(){
    var that = this
    var sorts = ''
    var asc = 'asc'
    var desc = 'desc'
    if(that.data.sort == desc){
      sorts = asc
    }else{
      sorts = desc
    }
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&typ=json&pagesize=1000',
      method: 'GET',
      data: {
        shop_id: that.data.shop_id,
        id: that.data.shop_id,
        order: 'common_price',
        sort: sorts,
        type_wxapp: 'wxapp'
        
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var good_info = res.data.data.items
        that.setData({
          goods: good_info,
          collect: "",
          salenum: "",
          price:"active",
          sort:sorts,
          types: "common_price",
          request_num: 0,
          currentNav: 'price' // 更新导航状态
        })
      }
    })
  },

  //跳转至商品上新
  update: function (e) {
    this.setData({
      currentNav: 'update'
    });
    wx.redirectTo({
      url: '../store_goods_update/store_goods_update?shop_id=' + this.data.shop_id
    })
  },

  //跳转店铺首页
  store: function (e) {
    this.setData({
      currentNav: 'store'
    });
    wx.redirectTo({
      url: '../store/store?shop_id=' + this.data.shop_id
    })
  },

  //搜索
  search: function () {
    wx.redirectTo({
      url: '../search_store/search_store?shop_id=' + this.data.shop_id
    })
  },

  /**
  * 页面上拉触底事件的处理函数
  */
  onReachBottom: function () {
    
    var that = this
    if (that.data.request_num == 1){
      return false
    }
    
    wx.request({
      url: app.configData.shop.api_url + '/index.php?ctl=Shop&met=goodsList&typ=json&pagesize=10',
      method: 'GET',
      data: {
        shop_id: that.data.shop_id,
        curpage: that.data.page,
        id: that.data.shop_id,
        type_wxapp: 'wxapp',
       
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        if (res.data.data.items.length > 0) {
          var arr = that.data.goods
          for (var z in res.data.data.items) {
            arr.push(res.data.data.items[z])
          }
        
          var page = Number(that.data.page + 1)
          that.setData({
            goods: arr,
            page: page
          })
        } else {
          that.setData({
            request_num: 1
          })
        }
      }
    })
  },
})